var reenabled = new Array();
var multigpu = new Array();

// Overclocking etc
// ----------------------------------------------------------

function togglePipelines(name,stockClock,stockShaderClock) {
    if (reenabled[name]) reenabled[name] = false;
    else reenabled[name] = true;

    if (reenabled[name]) {
        document.getElementById('reenabler' + name).value = 'Return to stock configuration';
    } else {
        document.getElementById('reenabler' + name).value = 'Enable Disabled Pipelines';
    }
    enhanceCoreClock(name,stockClock,0,stockShaderClock);
}

function toggleMultiGPU(name,stockCoreClock,stockMemClock,stockBandwidth,stockShaderClock) {
    if (multigpu[name]) multigpu[name] = false;
    else multigpu[name] = true;

    if (multigpu[name]) {
        document.getElementById('doubler' + name).value = 'Return to stock configuration';
    } else {
        document.getElementById('doubler' + name).value = 'Show Dual-Card Specs';
    }
    enhanceCoreClock(name,stockCoreClock,0,stockShaderClock);
    enhanceMemClock(name,stockMemClock,stockBandwidth,0);
}

function enhanceCoreClock(name,stockClock,change,stockShaderClock,flopsPerPipe) {
	flopsPerPipe = flopsPerPipe || 1;
  var currentClock = parseInt(document.getElementById('core_clock' + name).innerHTML);
  var newClock = currentClock + change;
  if (document.getElementById('spec_shader_clock' + name)) {
    var currentShaderClock = parseInt(document.getElementById('spec_shader_clock' + name).innerHTML);
    var newShaderClock = currentShaderClock + change;
    document.getElementById('spec_shader_clock' + name).innerHTML = newShaderClock;
  }

  document.getElementById('spec_core_clock' + name).innerHTML = newClock;
  document.getElementById('core_clock' + name).innerHTML = newClock;

  var temp = newClock - stockClock;
  if ((temp >= 0) && (temp <= 65)) {
    document.getElementById('enhance-core-scale' + name).style.width = temp + 'px';
  }

  if (multigpu[name]) var dualCard = 2;
  else var dualCard = 1;

  if (reenabled[name]) {
    if (document.getElementById('spec_raster_operators' + name)) {
      document.getElementById('spec_pixel_rate' + name).innerHTML = document.getElementById('spec_chip_raster_operators' + name).innerHTML * newClock * dualCard;
    } else {
      document.getElementById('spec_pixel_rate' + name).innerHTML = document.getElementById('spec_raster_operators' + name).innerHTML * newClock * dualCard;
    }
    if (document.getElementById('spec_chip_texture_units' + name)) {
      document.getElementById('spec_texel_rate' + name).innerHTML = document.getElementById('spec_chip_texture_units' + name).innerHTML * newClock * dualCard;
    } else {
      document.getElementById('spec_texel_rate' + name).innerHTML = document.getElementById('spec_texture_units' + name).innerHTML * newClock * dualCard;
    }
    if (document.getElementById('spec_chip_fragment_pipelines' + name)) {
      document.getElementById('spec_shader_rate' + name).innerHTML = document.getElementById('spec_chip_fragment_pipelines' + name).innerHTML * newClock * dualCard;
    } else {
      document.getElementById('spec_shader_rate' + name).innerHTML = document.getElementById('spec_fragment_pipelines' + name).innerHTML * newClock * dualCard;
    }
    if (document.getElementById('spec_chip_vertex_pipes' + name)) {
      document.getElementById('spec_vertex_rate' + name).innerHTML = document.getElementById('spec_chip_vertex_pipes' + name).innerHTML * newClock * dualCard / 4;
    } else {
      document.getElementById('spec_vertex_rate' + name).innerHTML = document.getElementById('spec_vertex_pipes' + name).innerHTML * newClock * dualCard / 4;
    }
  } else {
    document.getElementById('spec_texel_rate' + name).innerHTML = document.getElementById('spec_texture_units' + name).innerHTML * newClock * dualCard;
    if ((document.getElementById('spec_fragment_pipelines' + name)) && (document.getElementById('spec_vertex_pipes' + name))) {
      document.getElementById('spec_shader_rate' + name).innerHTML = document.getElementById('spec_fragment_pipelines' + name).innerHTML * newClock * dualCard;
      document.getElementById('spec_vertex_rate' + name).innerHTML = document.getElementById('spec_vertex_pipes' + name).innerHTML * newClock * dualCard / 4;
    } else {
      document.getElementById('spec_shader_rate' + name).innerHTML = document.getElementById('spec_unified_pipes' + name).innerHTML * newShaderClock * dualCard * flopsPerPipe / 1000;
    }
    document.getElementById('spec_pixel_rate' + name).innerHTML = document.getElementById('spec_raster_operators' + name).innerHTML * newClock * dualCard;
  }
  compareSpecs();
}

function enhanceMemClock(name,stockClock,stockBandwidth,change) {
  if (multigpu[name]) var dualCard = 2;
  else var dualCard = 1;

  var currentClock = parseInt(document.getElementById('mem_clock' + name).innerHTML);
  document.getElementById('mem_clock' + name).innerHTML = currentClock + change;
  document.getElementById('spec_mem_clock' + name).innerHTML = currentClock + change;
  if (document.getElementById('spec_eff_mem_clock' + name).innerHTML) document.getElementById('spec_eff_mem_clock' + name).innerHTML = (currentClock + change) * 2;

  var newBandwidth = stockBandwidth * (currentClock + change) / stockClock;
  var newBandwidth = Math.round(newBandwidth * 10) / 10;
  document.getElementById('spec_bandwidth' + name).innerHTML = newBandwidth * dualCard;

  var temp = parseInt(document.getElementById('mem_clock' + name).innerHTML) - stockClock;
  if ((temp >= 0) && (temp <= 65)) {
    document.getElementById('enhance-mem-scale' + name).style.width = temp + 'px';
  }
  compareSpecs();
}

function showEnhance(name,tabname) {
  document.getElementById('enhance-overclock' + name).style.display = 'none';
  document.getElementById('enhance-pipelines' + name).style.display = 'none';
  document.getElementById('enhance-multigpu' + name).style.display = 'none';
  document.getElementById('enhance-' + tabname + name).style.display = 'block';
  document.getElementById('enhance-tab-overclock' + name).style.borderBottom = '1px solid #b0b0b0';
  document.getElementById('enhance-tab-overclock' + name).style.backgroundImage = '';
  document.getElementById('enhance-tab-pipelines' + name).style.borderBottom = '1px solid #b0b0b0';
  document.getElementById('enhance-tab-pipelines' + name).style.backgroundImage = '';
  document.getElementById('enhance-tab-multigpu' + name).style.borderBottom = '1px solid #b0b0b0';
  document.getElementById('enhance-tab-multigpu' + name).style.backgroundImage = '';
  document.getElementById('enhance-tab-' + tabname + name).style.borderBottom = '';
  document.getElementById('enhance-tab-' + tabname + name).style.backgroundImage = 'url(images/enhance-bg.gif)';
}

// Titlebar buttons
// ----------------------------------------------------------

function cardMinimize(id) {
  document.getElementById(id).style.height = '0px';
}
function cardRestore(id) {
  document.getElementById(id).style.height = '100px';
}
function cardMaximize(id) {
  document.getElementById(id).style.height = '';
}

// Bold the spec of the better card
// ----------------------------------------------------------
function compareSpecs() {
  var specs = [ 'bandwidth', 'texel_rate', 'shader_rate', 'vertex_rate', 'pixel_rate' ];

  for (var i = 0; i < specs.length; i++) {
    var card1Spec = document.getElementById('spec_' + specs[i] + 'card1');
    var card2Spec = document.getElementById('spec_' + specs[i] + 'card2');
    if (!card1Spec || !card2Spec) continue;
    if (parseFloat(card1Spec.innerHTML) > parseFloat(card2Spec.innerHTML)) {
      card1Spec.style.fontWeight = 'bold';
      card2Spec.style.fontWeight = '';
    } else if (parseFloat(card1Spec.innerHTML) < parseFloat(card2Spec.innerHTML)) {
      card1Spec.style.fontWeight = '';
      card2Spec.style.fontWeight = 'bold';
    }
  }
}
if (window.addEventListener) window.addEventListener('load',compareSpecs,false);
else if (window.attachEvent) window.attachEvent('onload',compareSpecs);


// Retail Card Sorting
// ----------------------------------------------------------
var columnToSortBy = 0;
function sortRetailCards(column, cardId) {
  var cardTable = document.getElementById('retail-card-table-data-' + cardId);
  var cardTableBody = cardTable.childNodes[0];
  if (cardTableBody.tagName != 'TBODY') cardTableBody = cardTable.childNodes[1];

  var tableRows = new Array();

  var firstRow = null; // Use this to track the first TR encountered so we know it's the heading
  for (var i in cardTableBody.childNodes) {
    if (cardTableBody.childNodes[i].tagName == 'TR') {
      var tableRow = new Object();
      tableRow.contents = cardTableBody.childNodes[i].innerHTML;
      tableRow.cellsInRow = new Array();
      for (var j in cardTableBody.childNodes[i].childNodes) {
        if (cardTableBody.childNodes[i].childNodes[j].tagName == 'TD') {
          var cellContents = cardTableBody.childNodes[i].childNodes[j].innerHTML;
          tableRow.cellsInRow.push(cellContents);
        }
      }
      if (firstRow == null) firstRow = [ tableRow ];
      else tableRows.push(tableRow);
    }
  }

  columnToSortBy = column;
  if (columnToSortBy == 1) tableRows.sort(retailSorterNumeric);
  else if (columnToSortBy != 2) tableRows.sort(retailSorterNumericReverse);
  else tableRows.sort(retailSorter);
  tableRows = firstRow.concat(tableRows);


  var newTableHTML = "<table id=\"retail-card-table-data-" + cardId + "\" style=\"border: 0px; width: 100%;\" cellpadding=\"0\" cellspacing=\"0\"><tbody>";
  var count = 0;
  for (var row in tableRows) {
    if (count&1) rowClass = 'inner-table-oddrow';
    else rowClass = 'inner-table-evenrow';
    if (count == 0) rowClass = 'inner-table-header';
    newTableHTML += '<tr class="' + rowClass + '">' + tableRows[row].contents + "</tr>";
    count++;
  }
  newTableHTML += "</tbody></table";
  document.getElementById('retail-card-table-' + cardId).innerHTML = newTableHTML;
}

function retailSorter(a, b) {
  var c = a.cellsInRow[columnToSortBy].replace(/(<([^>]+)>)/ig,"").toLowerCase();
  var d = b.cellsInRow[columnToSortBy].replace(/(<([^>]+)>)/ig,"").toLowerCase();

  if ((c == '') && (d == '')) return 0;
  if (c == '') return 1;
  if (d == '') return -1;
  if (c < d) return -1;
  if (c > d) return 1;
  return 0;
}

function retailSorterNumeric(a, b) {
  var c = a.cellsInRow[columnToSortBy].replace(/(<([^>]+)>)/ig,"").replace('$','').replace(' R','');
  var d = b.cellsInRow[columnToSortBy].replace(/(<([^>]+)>)/ig,"").replace('$','').replace(' R','');

  if ((c == '') && (d == '')) return 0;
  if (c == '') return 1;
  if (d == '') return -1;
  return c - d;
}

function retailSorterNumericReverse(b, a) {
  var c = a.cellsInRow[columnToSortBy].replace(/(<([^>]+)>)/ig,"").replace(/[^\d]/g,'');
  var d = b.cellsInRow[columnToSortBy].replace(/(<([^>]+)>)/ig,"").replace(/[^\d]/g,'');

  if ((c == '') && (d == '')) return 0;
  if (c == '') return 1;
  if (d == '') return -1;
  return c - d;
}

// Debugging Console
var Debug = new Object();
Debug.show = function() {
  if (document.getElementById('debug')) { return; }
  var debug = document.createElement('div');
  debug.id = 'debug';
  debug.style.position = 'absolute';
  debug.style.left = '0px';
  debug.style.top = '0px';
  debug.style.backgroundColor = '#ffffff';
  debug.style.width = '300px';
  debug.style.fontSize = '10px';
  debug.style.color = '#000000';
  debug.style.height = '100px';
  debug.style.overflow = 'auto';
  var body = document.getElementsByTagName('body');
  body[0].appendChild(debug);
}
Debug.print = function(string) {
  this.show();
  document.getElementById('debug').innerHTML = string + '<br>' + document.getElementById('debug').innerHTML;
}
Debug.clear = function() {
  if (document.getElementById('debug')) {
    document.getElementById('debug').innerHTML = '';
  }
}
Debug.setText = function(string) {
  this.show();
  document.getElementById('debug').innerHTML = string;
}